<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../../assets/layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="../../assets/css/admin.css" />
		<link rel="stylesheet" href="../../assets/plugins/zTree_v3/css/zTreeStyle/zTreeStyle.css">
		
		
	</head>

	<body>
		<div style="margin: 30px;">
			<ul id="tree" class="ztree"></ul>
			<form class="layui-form" action="" method="post" onsubmit="return false;">
				<div class="layui-form-item">
					<input type="hidden" name="userId" id="userId">
					<input type="hidden" name="roleIds" id="roleIds">
				</div>
				<div class="layui-form-item">
					<input type="hidden" name="roleId" id="roleId">
					<input type="hidden" name="menuIds" id="menuIds">
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</form>
		<script type="text/javascript" src="../../js/common/jquery-1.10.2.min.js"></script>
		<script type="text/javascript" src="../../assets/layui/layui.js"></script>
		<script type="text/javascript" src="../../js/common/table.js"></script>
		<script type="text/javascript" src="../../js/common/commonUtil.js"></script>
		<script type="text/javascript" src="../../js/common/formUtil.js"></script>
		<script type="text/javascript" src="../../assets/plugins/zTree_v3/js/jquery.ztree.core-3.5.min.js"></script>
		<script type="text/javascript" src="../../assets/plugins/zTree_v3/js/jquery.ztree.excheck-3.5.min.js"></script>
		<script>
		    var path = "../..";
		    var type = "<%=request.getParameter("type") %>";
		    var id="<%=request.getParameter("id") %>"
			layui.use(['layer','form','layedit'], function() {
				var $ = layui.jquery,
					layer = layui.layer
					form = layui.form();
					var zNodes
					var qryUrl="";
					var saveUrl = "";
					var nodeUrl = "";
					if(type == "menu"){
						qryUrl = path + "/a/sys/MenuAction/queryAllMenus.do";
						nodeUrl = path + "/a/sys/RoleAction/quryRoleMenu.do"
						saveUrl = path + "/a/sys/RoleAction/saveRoleMenu.do";
					}else{
						qryUrl = path + "/a/sys/RoleAction/queryAllRoles.do";
						nodeUrl = path + "/a/sys/UserAction/quryUserRole.do"
						saveUrl = path + "/a/sys/UserAction/saveUserRole.do";
					}
					ComUtil.AjaxData("#tree",qryUrl,
						function($obj, data){
							zNodes = eval(data.data);
						}
					)
					var setting = {
							data: {
								simpleData: {
									enable: true
								}
							},
							check: {
								enable: true
							}
						};
				$.fn.zTree.init($("#tree"), setting, zNodes);
				var treeObj = $.fn.zTree.getZTreeObj("tree");
				
				ComUtil.AjaxData("#tree",nodeUrl,
						function($obj, data){
							if(data.data.length>0){
								for(var i=0;i<data.data.length;i++){
									if(type == "menu"){
										var node = treeObj.getNodeByParam("id", data.data[i].menuId);  
									    if (node) {  
									    	treeObj.checkNode(node, true, true);
									    } 
									}else{
										var node = treeObj.getNodeByParam("id", data.data[i].roleId);  
									    if (node) { 
									    	treeObj.checkNode(node, true, true);
									    } 
									}
									
								}
							}
						},{
							roleId:id,
							userId:id
						}
					)
				
				FromEngine.vsFrom(".layui-form", {
					debug : false,
					beforeSubmitFunction:function(){
						var nodes = treeObj.getCheckedNodes(true);
						var ids = "" 
						for (var i=0, l=nodes.length; i<l; i++) {
							ids += nodes[i].id + ";";
						}
						if(type == "menu"){
							$("#roleId").val(id);
							$("#menuIds").val(ids);
						}else{
							$("#userId").val(id);
							$("#roleIds").val(ids);
						}
						return true;
					},
					cfgUrlOrFunction : saveUrl,
					cfgCallBack : function(result) {
						ComUtil.closeParentLayer(function() {
						});
					}
				});
			});
		</script>
		</div>
	</body>
	
</html>